//
//  SettingViewController.m
//  MM
//
//  Created by 郑渊文 on 5/20/15.
//  Copyright (c) 2015 王晨辉. All rights reserved.
//

#import "SettingViewController.h"
#import "CommonMacro.h"
#import "SuggestViewController.h"
#import "AddBabyViewController.h"
#import <ShareSDK/ShareSDK.h>
#import <QZoneConnection/ISSQZoneApp.h>
#import <FacebookConnection/ISSFacebookApp.h>
#import "AboutUsViewController.h"
#import "LoginViewController.h"

@interface SettingViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UIButton *backBtn;
    UIButton *pushSwitch;
}
@property(nonatomic,copy)UITableView *myTable;
@property(nonatomic,copy)NSArray *setArray;//设置选项
@property(nonatomic,strong) UISwitch *mySwitch;
@property(nonatomic,copy) NSString *nowVersion;


@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置";
    [self initSubViews];
    pushSwitch.selected ==YES;
//    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]
//                                 initWithTitle:@"宝宝"
//                                 style:UIBarButtonItemStylePlain
//                                 target:self
//                                 action:@selector(turntoBaby)];
//    self.navigationItem.rightBarButtonItem = rightBtn;
    
     _setArray = @[/*@"消息推送",*/@"意见反馈",@"分享麦苗公学",@"检查更新",@"联系我们",@"关于麦苗公学"];
    self.nowVersion = @"版本号：1.00";
    
    
    self.view.backgroundColor = [UIColor yellowColor];
    
    // Do any additional setup after loading the view from its nib.
}
-(void)turntoBaby{
    
//    BabyViewController *babyVC = [[BabyViewController alloc]init];
//    
//    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:babyVC];
//    [self  presentViewController:nav animated:YES completion:nil];
    
}

-(void)initSubViews
{
    
    self.navigationItem.title = @"设置";
    [self.navigationController.navigationBar setTranslucent:NO];//设置navigationbar的半透明
    [self.navigationController.navigationBar setBarTintColor:[GetFileModel colorWithHexString:@"#34bd81" alpha:1]];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];

    self.view.backgroundColor = [UIColor clearColor];
    
    
    //自定义左右按钮
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [button setBackgroundImage:[UIImage imageNamed:@"_0000_返回.png"]
     
                      forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(back)
     
     forControlEvents:UIControlEventTouchUpInside];
    
    button.frame = CGRectMake(0, 0, 20, 15);
    
    
    
    UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    
    
    self.navigationItem.leftBarButtonItem = menuButton;
    
    _myTable = [[UITableView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    _myTable.delegate = self;
    _myTable.dataSource = self;
    _myTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_myTable];
    
    
    
    UIButton * btnClick = [UIButton buttonWithType:UIButtonTypeCustom];
    btnClick.frame = CGRectMake(15, SCREEN_HEIGHT-NavbarHight-80, SCREEN_WIDTH-30, 50);
    [btnClick setBackgroundImage:[UIImage imageNamed:@"gree@2x"] forState:UIControlStateNormal];
    [btnClick setBackgroundImage:[UIImage imageNamed:@"greeScl@2x"] forState:UIControlStateHighlighted];
    [btnClick setTitle:@"退出登录" forState:UIControlStateNormal];
    [btnClick setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btnClick.titleLabel.textAlignment = NSTextAlignmentCenter;
    [btnClick addTarget:self action:@selector(gota) forControlEvents:UIControlEventTouchUpInside];
    btnClick.userInteractionEnabled = YES;
    [self.view addSubview:btnClick];
    
    

    
    
}
-(void)gota{

    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"userCode"];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"ID"];
//    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"babyName"];
//    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"babyAge"];
    NSString *babyName = @"";
    NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
    [user1 setObject:babyName forKey:@"babyName"];
    
    NSString *babyAge = @"";
    NSUserDefaults *user2 = [NSUserDefaults standardUserDefaults];
    [user2 setObject:babyAge forKey:@"babyAge"];

    NSString *nasty = @"3";
    NSUserDefaults *user3 = [NSUserDefaults standardUserDefaults];
    [user3 setObject:nasty forKey:@"nasty"];

    [BabyManager shareInstance].babyId = nil;
    
    
    
    
    [[NSUserDefaults standardUserDefaults] synchronize];
    
//    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
//    
//    [defaults removeObjectForKey:@"UserInfo"];
//    
//    [defaults synchronize];
    

    
    LoginViewController * myTableView = [[LoginViewController alloc]init];
    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:myTableView];
    
    [self  presentViewController:nav animated:YES completion:nil];

    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark--- tableViewDelegate 有几个区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
    
}

#pragma mark--- tableViewDelegate 每个区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        return 3;
        
    }else {
        
        return 2;
    }
}

#pragma mark--- tableViewDelegate 区尾的高度

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 40;
    
}
#pragma mark--- tableViewDelegate cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *NOTIFY = @"cell";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:NOTIFY];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NOTIFY];
        

    }
    
//    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, _myTable.rowHeight-1, SCREEN_WIDTH, 1)];
//    view.backgroundColor = UIColorWithRGBA(229, 229, 231, 1);
//    [cell addSubview:view];

    cell.textLabel.text = _setArray[indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:15];

    if (indexPath.section==0) {
        if (indexPath.row==0) {
            
            pushSwitch = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-80,0,50,40)];
            [pushSwitch setImage:[UIImage imageNamed:@"开"] forState:UIControlStateNormal];
            [pushSwitch setImage:[UIImage imageNamed:@"关"] forState:UIControlStateSelected];
            [pushSwitch addTarget:self action:@selector(changePushSwitch:) forControlEvents:UIControlEventTouchUpInside];
//            [cell addSubview:pushSwitch];
            
//            self.mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-70, 10, 70, 30)];
//            //设置开关为YES状态
//            [self.mySwitch setOn:YES];
//            //为控件添加事件
//            [self.mySwitch addTarget:self action:@selector(switchIsChanged:) forControlEvents:UIControlEventValueChanged];
//            //向视图中添加该控件
//            [cell addSubview:self.mySwitch];
            
        }
        if (indexPath.row==3) {
            UILabel *visionLable = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-180, 5, 150, 30)];
            visionLable.text = _nowVersion;
            visionLable.textColor = [UIColor lightGrayColor];
            visionLable.textAlignment = NSTextAlignmentRight;
            [cell addSubview:visionLable];

        }

    }else if(indexPath.section==1){
        cell.textLabel.text = _setArray[indexPath.row+3];
        if (indexPath.row==0) {
            UILabel *phoneNum = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-180, 10, 160, 30)];
            phoneNum.text = @"0579-82388980";
            phoneNum.textAlignment = NSTextAlignmentRight;
            phoneNum.textColor = [UIColor lightGrayColor];
            [cell addSubview:phoneNum];
            

        }
        


    }

    
    if (indexPath.row!=0) {
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    }
        return cell;
}


- (void)changePushSwitch:(UIButton *)mySwitch
{
    mySwitch.selected = !mySwitch.selected;
    if (pushSwitch.selected == YES) {
        ////NSLog(@"=================关");
         [[UIApplication sharedApplication] unregisterForRemoteNotifications];
    }else if(pushSwitch.selected == NO)
    {
        ////NSLog(@"=================开");
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }
   
 
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section==0) {
        if (indexPath.row==0) {
            SuggestViewController *sugVC = [[SuggestViewController alloc]init];
            
            UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:sugVC];
            [self  presentViewController:nav animated:YES completion:nil];

        }
        if (indexPath.row==1) {

            [self ShowShareActionSheet];
        }
    }
    else if(indexPath.section==1)
    {
        if (indexPath.row==0) {
            
            UIAlertView *myarlt = [[UIAlertView alloc]initWithTitle:@"拨打电话" message:@"0579-82388980" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
            
            [myarlt show]; 
    
//            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://0579-82388980"]];
        }else if(indexPath.row == 1)
        {
            AboutUsViewController *sugVC = [[AboutUsViewController alloc]init];
            
            UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:sugVC];
            [self  presentViewController:nav animated:YES completion:nil];
        }else{
            

            
        }
    }

}


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==1) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://0579-82388980"]];
    }
    
}

-(void)back
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)ShowShareActionSheet {
    
    //1.定制分享的内容
    //https://www.baidu.com
    NSString* path = [[NSBundle mainBundle]pathForResource:@"share.png" ofType:nil];
    
    id<ISSContent> publishContent = [ShareSDK content:@"麦苗公学真是个好东西。。。" defaultContent:nil image:[ShareSDK imageWithPath:path] title:@"麦苗公学期待您的加入" url:@"https://www.baidu.com" description:@"This is description" mediaType:SSPublishContentMediaTypeNews];
    //2.调用分享菜单分享
    [ShareSDK showShareActionSheet:nil shareList:nil content:publishContent statusBarTips:YES authOptions:nil shareOptions:nil result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
        //如果分享成功
        if (state == SSResponseStateSuccess) {
            ////NSLog(@"分享成功");
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"麦苗公学" message:@"分享成功" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }
        //如果分享失败
        if (state == SSResponseStateFail) {
            //NSLog(@"分享失败,错误码:%ld,错误描述%@",(long)[error errorCode],[error errorDescription]);
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"麦苗公学" message:@"分享失败，请看日记错误描述" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }
    }];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
